package cn.wolfcode.wolf2w.listener;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVoRedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * redis缓存数据初始化监听器
 *
 *
 */
//@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyStatisVoRedisService statisVoRedisService;

    //当spring容器启动并初始化之后马上执行该方法
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

        System.out.println("====攻略vo初始化-启动=====");
        //查询mongodb的所有攻略
        List<Strategy> list = strategyService.list();

        //遍历所有攻略封装成vo对象
        for (Strategy strategy : list){

            //判断是否存在vo对象,存在就跳过不覆盖
            if (statisVoRedisService.isStrategyVoExist(strategy.getId())){
                continue;
            }
            StrategyStatisVO vo = new StrategyStatisVO();
            BeanUtils.copyProperties(strategy, vo);
            vo.setStrategyId(strategy.getId());

            statisVoRedisService.setStrategyStatisVo(vo);
        }

        System.out.println("====攻略vo初始化-结束====");

    }
}
